package com.devplatform.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class OssUtil {
    private static final String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    private static final String bucketName = "seecen-devplatform";
    private static final String accessKeyId = "LTAI5tLecQvLP8EyQ2XRVCsy";
    private static final String accessSecret = "XXQrtvl2imXqcUVSS936KuK81w7hHu";

    static {
        //检查桶是否存在
        OSS ossClient = getOSSClient();
        if (!ossClient.doesBucketExist(bucketName)) {
            ossClient.createBucket(bucketName);
        }
        //设置Bucket访问权限为PublicRead（允许所有人读取）
        ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    }

    private static OSS getOSSClient() {

        return new OSSClientBuilder().build(endpoint, accessKeyId, accessSecret);
    }


    public static String uploadFile(Long userId, MultipartFile multipartFile) throws IOException {
        OSS ossClient = getOSSClient();
        int lastIndex = multipartFile.getOriginalFilename().lastIndexOf("\\");
        String fileName = multipartFile.getOriginalFilename().substring(lastIndex + 1);
        // 2. 上传文件
        String objectName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH:mm:ss")) + fileName;
        ossClient.putObject(bucketName, userId + "/" + objectName, multipartFile.getInputStream());

        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + userId + "/" + objectName;
        System.out.println("文件 " + objectName + " 上传成功。 " + url);
        return url;
    }

    public static String adminUploadFile(FileInputStream fis, String name) throws IOException {
        OSS ossClient = getOSSClient();
        ossClient.putObject(bucketName, name, fis);

        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + name;
        System.out.println("文件 " + name + " 上传成功。 " + url);
        return url;
    }

    public static boolean adminDeleteFile(String objectName) {
        // 5. 删除文件
        OSS ossClient = getOSSClient();
        if (!ossClient.doesObjectExist(bucketName, objectName)) {
            return false;
        }
        ossClient.deleteObject(bucketName, objectName);
        System.out.println(objectName + " 删除成功。");
        return true;
    }

    public static boolean deleteFile(Long userId, String objectName) {
        // 5. 删除文件
        OSS ossClient = getOSSClient();
        if (!ossClient.doesObjectExist(bucketName, userId + objectName)) {
            return false;
        }
        ossClient.deleteObject(bucketName, userId + "/" + objectName);
        System.out.println(userId + "/" + objectName + " 删除成功。");
        return true;
    }

    public static List<String> getFiles() {
        OSS ossClient = getOSSClient();
        ObjectListing objectListing = ossClient.listObjects(bucketName);
        List<String> list = new ArrayList<>();
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + " (大小 = " + objectSummary.getSize() + ")");
            list.add(objectSummary.getKey());
        }
        return list;
    }

}
